﻿using System.Drawing;
using System.IO;

namespace TiaoTiaoCode.Common.Helpers
{
    /// <summary>
    /// 水印帮助类
    /// </summary>
    public class TiaoTiaoWaterMaker
    {
        //private string _waterMarkText;
        //private string _fontName;
        //private FontStyle _fontStyle;
        //private Color _color;
        //private int _maxFontSize;

        /// <summary>
        /// 单纯加文字水印
        /// </summary>
        /// <param name="markText"></param>
        /// <param name="path"></param>
        /// <param name="savepath"></param>
        public static void AddTextToImg(string markText, string path, string savepath)
        {
            Image image = Image.FromFile(path);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Font f = new Font("Verdana", 12);   
            Brush b = new SolidBrush(Color.Red);
            g.DrawString(markText, f, b, 20, image.Height - 100);
            g.Dispose();
            if (path.Equals(savepath))
            {
                image.Save(savepath + ".tmp");
                image.Dispose();
                File.Delete(path);
                File.Copy(savepath + ".tmp", savepath);
                File.Delete(savepath + ".tmp");
            }
            else
                image.Save(savepath);
        }

        /// <summary>
        /// 图片加文字水印
        /// </summary>
        /// <param name="markText"></param>
        /// <param name="fontSize"></param>
        /// <param name="fontName"></param>
        /// <param name="color"></param>
        /// <param name="position"></param>
        /// <param name="path"></param>
        /// <param name="savepath"></param>
        public static void AddTextToImg(string markText, int fontSize, string fontName, Color color, string position, string path, string savepath)
        {
            Image image = Image.FromFile(path);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            Font f = new Font(fontName, fontSize);
            Brush b = new SolidBrush(color);
            var ziSizeF = g.MeasureString(markText, f);

            float x = 0, y = 0;
            switch (position)
            {
                case "top-left":
                    x = ziSizeF.Width / 2f;
                    y = 8f;
                    break;
                case "top-center":
                    x = image.Width / 2f;
                    y = 8f;
                    break;
                case "top-right":
                    x = image.Width * 1f - ziSizeF.Width / 2f;
                    y = 8f;
                    break;
                case "bottom-right":
                    x = image.Width - ziSizeF.Width;
                    y = image.Height - ziSizeF.Height;
                    break;
                case "bottom-left":
                    x = ziSizeF.Width / 2f;
                    y = image.Height - ziSizeF.Height;
                    break;
                case "bottom-center":
                    x = image.Width / 2f;
                    y = image.Height - ziSizeF.Height;
                    break;
            }
            g.DrawString(markText, f, b, x, y);
            g.Dispose();
            if (path.Equals(savepath))
            {
                image.Save(savepath + ".tmp");
                image.Dispose();
                File.Delete(path);
                File.Copy(savepath + ".tmp", savepath);
                File.Delete(savepath + ".tmp");
            }
            else
                image.Save(savepath);
        }

        /// <summary>
        /// 加图片水印
        /// </summary>
        /// <param name="path"></param>
        /// <param name="markImgPath"></param>
        /// <param name="savepath"></param>
        public static void AddImgToImg(string path, string markImgPath, string savepath)
        {
            Image image = Image.FromFile(path);
            Image copyImage = Image.FromFile(markImgPath);
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g.Dispose();
            if (path.Equals(savepath))
            {
                image.Save(savepath + ".tmp");
                image.Dispose();
                File.Delete(path);
                File.Copy(savepath + ".tmp", savepath);
                File.Delete(savepath + ".tmp");
            }
            else
                image.Save(savepath);
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="waterMarkText">水印文字</param>
        ///// <param name="fontName">字体名</param>
        ///// <param name="maxFontSize">字体大小</param>
        ///// <param name="fontStyle">字体样式</param>
        ///// <param name="color">土桥村</param>
        ///// <param name="alpha">透明度</param>
        //public TiaoTiaoWaterMaker(string waterMarkText, string fontName, int maxFontSize, FontStyle fontStyle, Color color, byte alpha)
        //{
        //    _waterMarkText = waterMarkText;
        //    _fontName = fontName;
        //    _fontStyle = fontStyle;
        //    _color = Color.FromArgb(alpha, color);
        //    _maxFontSize = maxFontSize;
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="bitmap"></param>
        ///// <returns></returns>
        //public Bitmap Apply(Bitmap bitmap)
        //{
        //    Bitmap newBitmap = new Bitmap(bitmap);
        //    Graphics g = Graphics.FromImage(newBitmap);

        //    double tangent = (double)newBitmap.Height / (double)newBitmap.Width;
        //    double angle = Math.Atan(tangent) * (180 / Math.PI);
        //    double halfHypotenuse = Math.Sqrt((newBitmap.Height * newBitmap.Height) +
        //        (newBitmap.Width * newBitmap.Width)) / 2;
        //    StringFormat stringFormat = new StringFormat();
        //    stringFormat.Alignment = StringAlignment.Center;
        //    stringFormat.LineAlignment = StringAlignment.Center;

        //    Font font = new Font(_fontName, _maxFontSize, _fontStyle);
        //    for (int i = _maxFontSize; i > 0; i--)
        //    {
        //        font = new Font(_fontName, i, _fontStyle);
        //        SizeF sizef = g.MeasureString(_waterMarkText, font, int.MaxValue);

        //        double sin = Math.Sin(angle * (Math.PI / 180));
        //        double cos = Math.Cos(angle * (Math.PI / 180));

        //        double opp1 = sin * sizef.Width;
        //        double adj1 = cos * sizef.Height;

        //        double opp2 = sin * sizef.Height;
        //        double adj2 = cos * sizef.Width;

        //        if (opp1 + adj1 < newBitmap.Height &&
        //            opp2 + adj2 < newBitmap.Width)
        //        {
        //            break;
        //        }
        //    }
        //    g.SmoothingMode = SmoothingMode.AntiAlias;
        //    g.RotateTransform((float)angle);
        //    g.DrawString(_waterMarkText, font, new SolidBrush(_color),
        //        new Point((int)halfHypotenuse, 0), stringFormat);
        //    g.Dispose();
        //    return newBitmap;
        //}
    }
}
